<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Android笔记之使用CMake进行JNI开发环境需求 SDK Tools添加CMake  方式一，在创建工程时添加  在app目录下会有一个CMakeLists.txt文件  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# F">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://yoursite.com/2020/07/28/Android%E7%AC%94%E8%AE%B0%E4%B9%8B%E4%BD%BF%E7%94%A8CMake%E8%BF%9B%E8%A1%8CJNI%E5%BC%80%E5%8F%91/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="Android笔记之使用CMake进行JNI开发环境需求 SDK Tools添加CMake  方式一，在创建工程时添加  在app目录下会有一个CMakeLists.txt文件  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# F">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://i.loli.net/2020/07/28/tglqXKpLwBsbQyz.png">
<meta property="og:image" content="https://i.loli.net/2020/07/28/EXlAZFJnomcNBVa.png">
<meta property="article:published_time" content="2020-07-28T08:56:15.942Z">
<meta property="article:modified_time" content="2020-07-28T08:56:15.942Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2020/07/28/tglqXKpLwBsbQyz.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.0.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Android笔记之使用CMake进行JNI开发" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/07/28/Android%E7%AC%94%E8%AE%B0%E4%B9%8B%E4%BD%BF%E7%94%A8CMake%E8%BF%9B%E8%A1%8CJNI%E5%BC%80%E5%8F%91/" class="article-date">
  <time datetime="2020-07-28T08:56:15.942Z" itemprop="datePublished">2020-07-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="Android笔记之使用CMake进行JNI开发"><a href="#Android笔记之使用CMake进行JNI开发" class="headerlink" title="Android笔记之使用CMake进行JNI开发"></a>Android笔记之使用CMake进行JNI开发</h1><h2 id="环境需求"><a href="#环境需求" class="headerlink" title="环境需求"></a>环境需求</h2><blockquote>
<p>SDK Tools添加CMake</p>
</blockquote>
<h2 id="方式一，在创建工程时添加"><a href="#方式一，在创建工程时添加" class="headerlink" title="方式一，在创建工程时添加"></a>方式一，在创建工程时添加</h2><p><img src="https://i.loli.net/2020/07/28/tglqXKpLwBsbQyz.png" alt="这里写图片描述"></p>
<blockquote>
<p>在app目录下会有一个CMakeLists.txt文件</p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"># For more information about <span class="keyword">using</span> CMake with Android Studio, <span class="built_in">read</span> the</span><br><span class="line"><span class="meta"># documentation: https:<span class="comment">//d.android.com/studio/projects/add-native-code.html</span></span></span><br><span class="line"></span><br><span class="line"># Sets the minimum version of CMake required to build the native library.</span><br><span class="line"></span><br><span class="line">cmake_minimum_required(VERSION <span class="number">3.4</span><span class="number">.1</span>)</span><br><span class="line"></span><br><span class="line"># Creates <span class="keyword">and</span> names a library, sets it as either STATIC</span><br><span class="line"><span class="meta"># or SHARED, and provides the relative paths to its source code.</span></span><br><span class="line"># You can define multiple libraries, <span class="keyword">and</span> CMake builds them <span class="keyword">for</span> you.</span><br><span class="line"># Gradle automatically packages shared libraries with your APK.</span><br><span class="line"></span><br><span class="line"># 配置so库信息</span><br><span class="line">add_library( # Sets the name of the library.</span><br><span class="line">            # 生成的so库名称，此处生成的so文件名称是libnative-lib.so</span><br><span class="line">             native-lib</span><br><span class="line"></span><br><span class="line">             # Sets the library as a shared library.</span><br><span class="line">             # STATIC：静态库，是目标文件的归档文件，在链接其它目标的时候使用</span><br><span class="line">             # SHARED：动态库，会被动态链接，在运行时被加载</span><br><span class="line">             # MODULE：模块库，是不会被链接到其它目标中的插件，但是可能会在运行时使用dlopen-系列的函数动态链接</span><br><span class="line">             SHARED</span><br><span class="line"></span><br><span class="line">             # Provides a relative path to your source file(s).</span><br><span class="line">             # 资源文件，可以多个，</span><br><span class="line">             # 资源路径是相对路径，相对于本CMakeLists.txt所在目录</span><br><span class="line">             src/main/cpp/native-lib.cpp )</span><br><span class="line"></span><br><span class="line"># Searches <span class="keyword">for</span> a specified prebuilt library <span class="keyword">and</span> stores the path as a</span><br><span class="line"><span class="meta"># variable. Because CMake includes system libraries in the search path by</span></span><br><span class="line"><span class="meta"># default, you only need to specify the name of the public NDK library</span></span><br><span class="line"><span class="meta"># you want to add. CMake verifies that the library exists before</span></span><br><span class="line"><span class="meta"># completing its build.</span></span><br><span class="line"></span><br><span class="line"># 从系统查找依赖库</span><br><span class="line">find_library( # Sets the name of the path variable.</span><br><span class="line">              # android系统每个类型的库会存放一个特定的位置，而<span class="built_in">log</span>库存放在<span class="built_in">log</span>-lib中</span><br><span class="line">              <span class="built_in">log</span>-lib</span><br><span class="line"></span><br><span class="line">              # Specifies the name of the NDK library that</span><br><span class="line">              # you want CMake to locate.</span><br><span class="line">              # android系统在c环境下打<span class="built_in">log</span>到logcat的库</span><br><span class="line">              <span class="built_in">log</span> )</span><br><span class="line"></span><br><span class="line"># Specifies libraries CMake should link to your target library. You</span><br><span class="line"><span class="meta"># can link multiple libraries, such as libraries you <span class="meta-keyword">define</span> in this</span></span><br><span class="line"><span class="meta"># build script, prebuilt third-party libraries, or system libraries.</span></span><br><span class="line"></span><br><span class="line"># 配置库的链接（依赖关系）</span><br><span class="line">target_link_libraries( # Specifies the target library.</span><br><span class="line"></span><br><span class="line">                       # 目标库</span><br><span class="line">                       native-lib</span><br><span class="line"></span><br><span class="line">                       # Links the target library to the <span class="built_in">log</span> library</span><br><span class="line">                       # included in the NDK.</span><br><span class="line">                       # 依赖于</span><br><span class="line">                       $&#123;<span class="built_in">log</span>-lib&#125; )</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在app/build.gradle中有</p>
</blockquote>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// android下</span></span><br><span class="line">    externalNativeBuild &#123;</span><br><span class="line">        cmake &#123;</span><br><span class="line">            path <span class="string">&quot;CMakeLists.txt&quot;</span> <span class="comment">// cmake配置文件路径</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="方法二，在项目开发过程中添加jni"><a href="#方法二，在项目开发过程中添加jni" class="headerlink" title="方法二，在项目开发过程中添加jni"></a>方法二，在项目开发过程中添加jni</h2><blockquote>
<p>首先，在app下新建一个CMakeLists.txt。find_library、target_link_libraries不是必须的，可以省略。</p>
<p>cmake_minimum_required(VERSION 3.6)</p>
<p>add_library( # Sets the name of the library.<br>             xjni</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># Sets the library as a shared library.</span><br><span class="line">SHARED</span><br><span class="line"></span><br><span class="line"># Provides a relative path to your source file(s).</span><br><span class="line">src/main/jni/XJni.c )</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>在app/build.gradle中添加</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; android下</span><br><span class="line">    externalNativeBuild &#123;</span><br><span class="line">        cmake &#123;</span><br><span class="line">            path &quot;CMakeLists.txt&quot; &#x2F;&#x2F; cmake配置文件路径</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>Sycn</p>
<p>编译完成，编译器会报找不到XJni.c文件错误。ok，那我们新建一个/app/src/main/jni/XJni.c：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;jni.h&gt;</span></span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>新建java文件调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">XJni</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.loadLibrary(<span class="string">&quot;xjni&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">native</span> String <span class="title">getStr</span><span class="params">(String s)</span></span>;</span><br></pre></td></tr></table></figure>

<p>getStr方法显示错误红色，不用着急，我们选中函数名，按快捷键alt+enter：</p>
<p><img src="https://i.loli.net/2020/07/28/EXlAZFJnomcNBVa.png" alt="这里写图片描述"></p>
<p>选择create function后，函数就自动在XJni.c文件中生成了。</p>
</blockquote>
<p>需要注意的是，你最好让.java文件名与.c文件名同名，否则你可能快捷键不会出现create function选项。修改.c文件名的时候记得对应将CMakeLists.txt中修改。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/07/28/Android%E7%AC%94%E8%AE%B0%E4%B9%8B%E4%BD%BF%E7%94%A8CMake%E8%BF%9B%E8%A1%8CJNI%E5%BC%80%E5%8F%91/" data-id="ckdtkxkwl0001z0ujhpet3mld" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/07/31/Android%E5%9C%A8TextView%E4%B8%AD%E8%AE%BE%E7%BD%AE%E9%83%A8%E5%88%86%E6%96%87%E5%AD%97%E5%8F%AF%E7%82%B9%E5%87%BB/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2020/07/27/%E6%9E%9A%E4%B8%BE%E5%9E%8B%20%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/07/">July 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/08/14/MY-First-Post/">MY First Post</a>
          </li>
        
          <li>
            <a href="/2020/08/14/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/2020/07/31/RecyclerView%20%E6%92%91%E6%BB%A1/">(no title)</a>
          </li>
        
          <li>
            <a href="/2020/07/31/RecyclerView%20Adapter/">(no title)</a>
          </li>
        
          <li>
            <a href="/2020/07/31/Android%E5%9C%A8TextView%E4%B8%AD%E8%AE%BE%E7%BD%AE%E9%83%A8%E5%88%86%E6%96%87%E5%AD%97%E5%8F%AF%E7%82%B9%E5%87%BB/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>